<template>
  <div>
    <FilterTable
      ref="table"
      columnsRef="reportPartBreakskk"
      :columns="columns"
      selectionType="single"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="arr => (this.selectColumns = arr)"
      :isExportExcel="true"
      :onCalcExportData="exportFun"
      exportFileName="配件综合情况查询-配件报损.xlsx"
      :dataSource="dataSource"
      :isMemoryPagination="true"
    >
      <!-- :fetchapi="fetchApi"
    :params="fetchParams"
    :datakey="'rows'" -->
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :click="printHandle">打印</AjaxButton>
      </template>
      <template slot="controls">
        <AjaxButton size="small" type="primary" icon="el-icon-printer" :click="exportHandle">导出</AjaxButton>
      </template>
    </FilterTable>
    <!-- 打印 -->
    <div v-if="showPrint">
      <BasePrint ref="print" :data="printList" template="templatePartBreak" />
    </div>
  </div>
</template>
<script>
import { dictionary } from '@/utils/dictMixin';
import { queryPartInOutStockReport, getAllUser, exportPartInOutStockReport } from '@/api/part/report/billSearch';
import { getStroageList, getPrintData } from '@/api/part/partOutbound/partBreak';
import moment from 'moment';
import { exportExcelFile } from '@/utils/index';
export default {
  mixins: [dictionary],
  props: {
    fetchParamObj: Object,
    type: String,
    userList: Array,
    tax: Number
  },

  data() {
    return {
      fetchApi: queryPartInOutStockReport,
      columns: this.createdTable(),
      fetchParams: { xhrAbort: true },
      selectColumns: [],
      printList: {},
      dataSource: [],
      showPrint: true
    };
  },
  watch: {
    fetchParamObj(fetchParamObj) {
      // console.log(fetchParamObj, 'fetchParamObj----');
      this.initTableData(fetchParamObj);
    }
  },
  mounted() {
    this.getStorage();
    // console.log(this.tax, '税率');

    this.getEmployee();
  },
  methods: {
    // 导出方法
    exportFun(row) {
      if (!row.fake6) {
        row.fake6 = Number(row.COST_PRICE * (1 + Number(this.tax))).toFixed(2);
      }
      if (!row.fake7) {
        row.fake7 = Number(row.COST_AMOUNT * (1 + Number(this.tax))).toFixed(2);
      }
      for (const key in row) {
        switch (key) {
          // 格式化日期
          // case 'FINISHED_DATE':
          //   // case 'FINISHED_DATE':
          //   // case 'BALANCE_TIME':
          //   // case 'SQUARE_DATE':
          //   if (row[key]) {
          //     row[key] = moment(row[key]).format('YYYY-MM-DD');
          //   }
          //   break;
          // 格式化人名
          case 'CONSULTANT':
            if (row[key]) {
              row[key] = this.userList[row[key]];
            }
            break;
          // 格式化金额
          case 'COST_PRICE':
          case 'COST_AMOUNT':
            if (row[key]) {
              row[key] = Number(row[key]).toFixed(2);
            }
            break;
          default:
            break;
        }
      }
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 70,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        { title: '出库单编号', dataIndex: 'LOSS_NO', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '对应单编号', dataIndex: 'INVENTORY_NO', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '对应单编号', dataIndex: 'fake1', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '出库日期', dataIndex: 'FINISHED_DATE', sorter: true, filter: true, filterType: 'date-range', showOverflowTooltip: true, exportFormat: { t: 'd' } },
        // { title: '客户名称', dataIndex: 'fake2', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '牌照号', dataIndex: 'fake3', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '仓库名称', dataIndex: 'STORAGE_CODE', dictItems: [], editItems: [], sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '税率(%)', dataIndex: 'fake4', sorter: true, filter: true, filterType: 'input' },
        { title: '操作员代码', dataIndex: 'HANDLER', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        {
          title: '操作员名称',
          dataIndex: 'OPERATOR_NAME',
          render: props => {
            return <span>{this.userList[props.row.OPERATOR_NAME]}</span>;
          },
          dictItems: [],
          sorter: true,
          filter: true,
          filterType: 'input',
          showOverflowTooltip: true
        },
        { title: '配件代码', dataIndex: 'PART_NO', width: '160', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '配件名称', dataIndex: 'PART_NAME', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // {
        //   title: '配件说明',
        //   dataIndex: 'PART_DESCRIPTION',
        //   sorter: true,
        //   showOverflowTooltip: true,
        //   filter: true,
        //   filterType: 'input',
        //   showOverflowTooltip: true
        // },
        // { title: 'CCC件类型', dataIndex: 'IS_CCC_PART', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '保质期', dataIndex: 'SHELF_LIFT_MARK' },
        // {
        //   title: 'ABC分类',
        //   dataIndex: 'ABC_TYPE',
        //   sorter: true,
        //   filter: true,
        //   filterType: 'checkbox',
        //   filterItems: this.createDictList('8092'),
        //   dictItems: this.createDictList('8092'),
        //   render: props => {
        //     return <span>{this.createDictText(props.row.ABC_TYPE, '8092')}</span>;
        //   },
        //   showOverflowTooltip: true
        // },
        // { title: '计量单位', dataIndex: 'UNIT_NAME', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        { title: '出库数量', dataIndex: 'LOSS_QUANTITY', summation: true, sorter: true, filter: true, filterType: 'number', showOverflowTooltip: true, exportFormat: { t: 'n' } },
        {
          title: '含税出库单价',
          dataIndex: 'fake6',
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2,
          summation: false,
          render: props => {
            props.row.fake6 = Number(props.row.COST_PRICE).toFixed(2);
            return <span>{props.row.fake6}</span>;
          },
          showOverflowTooltip: true,
          exportFormat: { t: 'n' }
        },
        // { title: '含税成本单价', dataIndex: 'COST_PRICE', sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true, exportFormat: { t: 'n' } },
        {
          title: '含税出库金额',
          dataIndex: 'fake7',
          summation: true,
          sorter: true,
          filter: true,
          filterType: 'number',
          precision: 2,
          render: props => {
            props.row.fake7 = Number(props.row.COST_AMOUNT).toFixed(2);
            return <span>{props.row.fake7}</span>;
          },
          showOverflowTooltip: true,
          exportFormat: { t: 'n' }
        },
        // { title: '含税成本金额', dataIndex: 'COST_AMOUNT', summation: true, sorter: true, filter: true, filterType: 'number', precision: 2, showOverflowTooltip: true, exportFormat: { t: 'n' } }
        // { title: '主组号', dataIndex: 'MAIN_GROUP_NO', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '销售方式', dataIndex: 'fake8', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true },
        // { title: '发布单号', dataIndex: 'fake9', sorter: true, filter: true, filterType: 'input', showOverflowTooltip: true }

        // {
        //   title: '报损单号',
        //   dataIndex: 'LOSS_NO',
        //   sorter: true
        // },
        // {
        //   title: '盘点单号',
        //   dataIndex: 'INVENTORY_NO',
        //   sorter: true
        // },
        // {
        //   title: '入帐日期',
        //   dataIndex: 'FINISHED_DATE',
        //   sorter: true,
        //   width: '160'
        // },
        // {
        //   title: '成本金额',
        //   dataIndex: 'COST_AMOUNT',
        //   sorter: true
        // },
        // {
        //   title: '出库金额',
        //   dataIndex: 'LOSS_AMOUNT',
        //   sorter: true
        // },
        // {
        //   title: '操作员名称',
        //   dataIndex: 'HANDLER',
        //   sorter: true,
        //   editable: false,
        //   editType: 'select',
        //   editItems: []
        // },

        // {
        //   title: '仓库',
        //   dataIndex: 'STORAGE_CODE',
        //   sorter: true,
        //   editable: false,
        //   editType: 'select',
        //   editItems: []
        // }
      ];
    },

    async initTableData(obj) {
      const fetchParamObj = this.fetchParamObj;
      // this.fetchParams = Object.assign({}, fetchParamObj, { xhrAbort: false });
      this.fetchParams = Object.assign({}, fetchParamObj);
      try {
        this.$refs.table.START_LOADING();
        this.dataSource = [];
        let res = await queryPartInOutStockReport(this.fetchParams);
        this.dataSource = res.data;
        this.$refs.table.STOP_LOADING();
      } catch (error) {
        console.log(error);
      } finally {
        this.$refs.table.STOP_LOADING();
      }
    },

    // 获取仓库
    async getStorage() {
      const res = await getStroageList();
      this.columns.find(x => x.dataIndex === 'STORAGE_CODE').dictItems = this.columns.find(x => x.dataIndex === 'STORAGE_CODE').editItems = res.data.map(x => ({
        text: x.STORAGE_NAME,
        value: x.STORAGE_CODE + ''
      }));
    },
    // 打印数据接口
    async getPrintData() {
      // console.log(this.selectColumns[0].PART_ALLOCATE_IN_ID);
      const res = await getPrintData(this.selectColumns[0].LOSS_NO); // 获取当前行数据
      this.printList.items = res.data;
    },
      //导出
    async exportHandle(){
      let res = await exportPartInOutStockReport(this.fetchParams);
      console.log(res,"导出::");
      try{
        exportExcelFile(res.data,'配件报损')
      }catch(e){
        console.log(e,"错误::");
      }
    },
    // 打印处理
    async printHandle() {
      // this.getPrintParams();
      // 判断是否选中数据
      if (this.selectColumns.length < 1) {
        return this.$notify.error({ title: '提示信息', message: '请选中一条数据' });
      }
      await this.getPrintData();
      this.showPrint = true;
      console.log(this.printList, '::::');
      this.printList.lossNo = this.selectColumns[0].LOSS_NO;
      // todo element.baseInfo.RECEIPTOR_NAME = departmentList[element.baseInfo.ORG_CODE];
      // 打印
      this.$nextTick(() => {
        // console.log(123);

        this.$refs.print.EXCUTE_PRINT();
      });
    },
    // 操作员名称
    async getEmployee() {
      const res = await getAllUser();
      // console.log(res, '操作员名称');
      if (res.resultCode == 200) {
        this.columns.find(item => item.dataIndex === 'OPERATOR_NAME').dictItems = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      }
      // this.columns.find(item => item.dataIndex === 'HANDLER').editItems = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
    }
  }
};
</script>

<style lang="less" scoped>
/* .form-group {
  margin-top: 10px;
  span {
    height: 50px;
    line-height: 50px;
    text-align: right;
    width: 250px;
    padding-left: 100px;
  }
} */
</style>
